<template>
  <loading v-if="loading"></loading>
  <div v-else>
    <div class="top-box">
      <el-button type="warning" @click="addrole">添加角色</el-button>
    </div>
    <div  class="pd">
      <el-table :data="roles.list" border fit highlight-current-row >
        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="标识" prop="slug"></el-table-column>
        <el-table-column label="创建时间" prop="created_at"></el-table-column>
        <el-table-column label="更新时间" prop="updated_at"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <i class="icon-auth-set i-icon" title="查看" @click="check(scope.row.id,scope.row.name)"></i>
            <i class="icon-revamp i-icon"  title="修改"  @click="edit(scope.row)" v-if="scope.row.name!=='Administrator'"></i>
            <i class="icon-delete i-icon" title="删除" @click="del(scope.row.id)" v-if="scope.row.name!=='Administrator'"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="roles.total"
          v-if="roles.total>0">
        </el-pagination>
      </div>
      <!--添加角色-->
      <el-dialog title="添加角色" :visible.sync="add.addShow">
        <div class="input-group">
          <label>名称：</label>
          <div class="input-group-box"><el-input v-model="add.name" placeholder="输入名称"></el-input></div>
        </div>
        <div class="input-group">
          <label>标识：</label>
          <div class="input-group-box"><el-input v-model="add.slug"  placeholder="输入标识"></el-input></div>
        </div>
        <div style="margin-top:20px;text-align:right">
          <el-button  @click="r_cancel">取消</el-button>
          <el-button type="primary" @click="r_add">确定</el-button>
        </div>
      </el-dialog>
      <!--修改角色-->
      <el-dialog title="修改角色" :visible.sync="revise.rShow">
        <div class="input-group">
          <label>名称：</label>
          <div class="input-group-box"><el-input v-model="revise.name" placeholder="输入名称"></el-input></div>
        </div>
        <div class="input-group">
          <label>标识：</label>
          <div class="input-group-box"><el-input v-model="revise.slug"  placeholder="输入标识"></el-input></div>
        </div>
        <div style="margin-top:20px;text-align:right">
          <el-button  @click="revise_cancel">取消</el-button>
          <el-button type="primary" @click="revise_q">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<style>
</style>
<script>
import config from '../../utils/config'
import crypt from '../../utils/crypto-helper-pkcs7'
import axios from 'axios'
import loading from '../../components/loading'
export default {
  data () {
    return {
      pageall: 1,
      page: 1,
      pageSize: 20,
      roles: [],
      add: {
        addShow: false,
        name: '',
        slug: ''
      },
      revise: {
        id: '',
        rShow: false,
        name: '',
        slug: ''
      },
      loading: true
    }
  },
  components: {
    loading
  },
  created () {
    this.getrole()
  },
  methods: {
    getrole () {
      var param = {
        page: this.page
      }
      var paramjson = JSON.stringify(param)
      var params = crypt.encrypt(paramjson)
      axios.get(config.domain + config.urls.rolelist + '?param=' + params)
      .then(res => {
        var response = res.data
        if (response.errcode === 200) {
          this.roles = response.data
          this.loading = false
        } else if (response.errcode === 200103) {
          this.$confirm('登录过期，请重新登录')
          .then(_ => {
            window.sessionStorage.clear()
            this.$router.push('/admin/login')
          })
          .catch(_ => {})
        }
      })
      .catch(error => {
        console.log(error)
      })
    },
    handleCurrentChange (val) { // 分页
      this.page = val
      this.getrole()
    },
    addrole () { // 弹出添加角色模态框
      this.add.addShow = true
      this.add.name = ''
      this.add.slug = ''
    },
    r_cancel () { // 取消添加角色
      this.add.addShow = false
    },
    r_add () { // 添加角色
      var param = {
        name: this.add.name,
        slug: this.add.slug
      }
      var paramjson = JSON.stringify(param)
      var params = crypt.encrypt(paramjson)
      axios.get(config.domain + config.urls.roleadd + '?param=' + params)
      .then(res => {
        var response = res.data
        if (response.errcode === 200) {
          this.getrole()
          this.add.addShow = false
          this.$notify({
            title: '成功',
            message: '角色添加成功',
            type: 'success',
            duration: 1500
          })
        } else if (response.errcode === 200103) {
          this.$confirm('登录过期，请重新登录')
          .then(_ => {
            window.sessionStorage.clear()
            this.$router.push('/admin/login')
          })
          .catch(_ => {})
        } else {
          this.$notify({
            title: '失败',
            message: '角色添加失败',
            type: 'error',
            duration: 1500
          })
        }
      })
      .catch(error => {
        console.log(error)
      })
    },
    edit (row) { // 弹出编辑角色模态框
      this.revise.rShow = true
      this.revise.id = row.id
      this.revise.name = row.name
      this.revise.slug = row.slug
    },
    revise_cancel () { // 取消编辑角色
      this.revise.rShow = false
    },
    revise_q () { // 修改编辑角色
      var param = {
        id: this.revise.id,
        name: this.revise.name,
        slug: this.revise.slug
      }
      var paramjson = JSON.stringify(param)
      var params = crypt.encrypt(paramjson)
      axios.get(config.domain + config.urls.roleupdate + '?param=' + params)
      .then(res => {
        var response = res.data
        if (response.errcode === 200) {
          this.getrole()
          this.revise.rShow = false
          this.$notify({
            title: '成功',
            message: '角色修改成功',
            type: 'success',
            duration: 1500
          })
        } else if (response.errcode === 200103) {
          this.$confirm('登录过期，请重新登录')
          .then(_ => {
            window.sessionStorage.clear()
            this.$router.push('/admin/login')
          })
          .catch(_ => {})
        } else {
          this.$notify({
            title: '失败',
            message: '角色修改失败',
            type: 'error',
            duration: 1500
          })
        }
      })
      .catch(error => {
        console.log(error)
      })
    },
    del (id) { // 删除该角色
      var param = {
        id: id
      }
      var paramjson = JSON.stringify(param)
      var params = crypt.encrypt(paramjson)
      this.$confirm('确定删除该角色吗？')
      .then(_ => {
        axios.get(config.domain + config.urls.roledel + '?param=' + params)
        .then(res => {
          var response = res.data
          if (response.errcode === 200) {
            this.getrole()
            this.$notify({
              title: '成功',
              message: '角色删除成功',
              type: 'success',
              duration: 1500
            })
          } else if (response.errcode === 200103) {
            this.$confirm('登录过期，请重新登录')
            .then(_ => {
              window.sessionStorage.clear()
              this.$router.push('/admin/login')
            })
            .catch(_ => {})
          } else {
            this.$notify({
              title: '失败',
              message: '角色删除失败',
              type: 'error',
              duration: 1500
            })
          }
        })
        .catch(error => {
          console.log(error)
        })
      })
      .catch(_ => {})
    },
    check (id, role) {
      var crole = crypt.encrypt(role)
      this.$router.push('/admin/index/system/modifyRight/' + id + '?role=' + crole)
    }
  }
}
</script>

